package com.st.vmall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.vmall.entity.Admin;
import com.st.vmall.mapper.AdminMapper;
import com.st.vmall.service.IAdminService;
import com.st.vmall.util.MD5Util;

@Service
public class AdminServiceImpl implements IAdminService{
	
	@Autowired
	private AdminMapper adminMapper;

	@Override
	public Admin login(Admin admin) {
		admin.setPassword(MD5Util.getMD5(admin.getPassword()));
		return adminMapper.selectByNameAndPwd(admin);
	}

	@Override
	public int modifyPwd(Admin admin, String oldpassword, String newpassword, String newpassword2) {
		//判断旧密码是否正确
		if (!admin.getPassword().equals(MD5Util.getMD5(oldpassword))) {
			return -2;
		}
		
//		if (!admin.getPassword().equals(oldpassword)) {
//			return -2;
//		}
		
		//两次密码是否一致
		if (!newpassword.equals(newpassword2)) {
			return -3;
		}
		
		Admin updateAdmin = new Admin();
		updateAdmin.setId(admin.getId());
		updateAdmin.setPassword(MD5Util.getMD5(newpassword));
		
		//修改
		
		return adminMapper.update(updateAdmin);
	}
	@Override
	public int modify(Admin admin) {
		
		return adminMapper.update(admin);
	}
}
